package com.zerowidth.moc.web.controller;

import com.zerowidth.db.bean.ReportBean;
import com.zerowidth.db.bean.SignBean;
import com.zerowidth.moc.web.auth.annotation.NeedToken;
import com.zerowidth.moc.web.auth.annotation.NoToken;
import com.zerowidth.moc.web.bean.LoginUserVM;
import com.zerowidth.moc.web.controller.service.AccountService;
import com.zerowidth.moc.web.resp.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class AccountController {

    @Autowired
    AccountService accountService;


    ///////////////////////   登录   ////////////////////////////
    //////////////////////////////////////////////////////
    @NoToken
    @GetMapping("/login/{phone}")
    public ResponseEntity<ResultBean<LoginUserVM>> login(
            @PathVariable("phone") String phone,
            @RequestParam("authCode") String authCode,
            @RequestParam("pwd_key_md5") String pwd_key_md5,
            @RequestParam(value = "brand", required = false) String brand,
            @RequestParam(value = "model", required = false) String model) {
        return accountService.login(phone, authCode, pwd_key_md5, brand, model);
    }

    @NoToken
    @GetMapping("/login/sns")
    public ResponseEntity<ResultBean<LoginUserVM>> loginSNS_WX(
            HttpServletRequest request,
            @RequestParam("unionid") String unionid,
            @RequestParam("nickname") String nickname,
            @RequestParam("headimgurl") String headimgurl) {
        return accountService.loginSNS_WX(request, unionid, nickname, headimgurl);
    }

    @NoToken
    @GetMapping("/login/sns_QQ")
    public ResponseEntity<ResultBean<LoginUserVM>> loginSNS_QQ(
            HttpServletRequest request,
            @RequestParam("qq_open_id") String qq_open_id,
            @RequestParam("nickname") String nickname,
            @RequestParam("headimgurl") String headimgurl) {
        return accountService.loginSNS_QQ(request, qq_open_id, nickname, headimgurl);
    }

    @NoToken
    @GetMapping("/login/userId8")
    public ResponseEntity<ResultBean<LoginUserVM>> loginUserId8(
            HttpServletRequest request,
            @RequestParam("user_id") String user_id) {
        return accountService.loginUserId8(request, user_id);
    }

    @NoToken
    @GetMapping("/login/auto_register")
    public ResponseEntity<ResultBean<LoginUserVM>> loginAutoRegister(
            HttpServletRequest request) {
        return accountService.loginAutoRegister(request);
    }

    @NoToken
    @GetMapping("/login/stat")
    public ResponseEntity<ResultBean<LoginUserVM>> loginStatId(
            HttpServletRequest request) {
        return accountService.loginStatId(request);
    }

    ///////////////////////   创建账号   ////////////////////////////
    //////////////////////////////////////////////////////
    @NoToken
    @GetMapping("/createUser/{phone}")
    public ResponseEntity<ResultBean<LoginUserVM>> createUserByPhone(
            @PathVariable("phone") String phone,
            @RequestParam("authCode") String authCode) {
        return accountService.createUserByPhone(phone, authCode);
    }

    @NoToken
    @GetMapping("/register")
    public ResponseEntity<ResultBean<LoginUserVM>> createUserByRegister(
            @RequestParam("statid") String statid,
            @RequestParam("username") String userName,
            @RequestParam("userpwd") String userPwd) {
        return accountService.createUserByRegister(statid, userName, userPwd);
    }

    @NoToken
    @GetMapping("/createUser/wx")
    public ResponseEntity<ResultBean<LoginUserVM>> createUserByWx(
            @RequestParam("unionid") String unionid,
            @RequestParam("nickname") String nickname,
            @RequestParam("headimgurl") String headimgurl) {
        return accountService.createUserByWX(unionid, nickname, headimgurl);
    }

    @NoToken
    @GetMapping("/createUser/qq")
    public ResponseEntity<ResultBean<LoginUserVM>> createUserByQQ(
            HttpServletRequest request,
            @RequestParam("qqOpenId") String qqOpenId,
            @RequestParam("nickname") String nickname,
            @RequestParam("headimgurl") String headimgurl) {
        return accountService.createUserByQQ(request, qqOpenId, nickname, headimgurl);
    }


    ///////////////////////   发送验证码   ////////////////////////////
    //////////////////////////////////////////////////////
    @NoToken
    @GetMapping("/sendAuthCodeSMS/{phone}")
    public ResponseEntity<ResultBean<String>> sendAuthCodeSMS(
            HttpServletRequest request,
            @PathVariable("phone") String phone,
            @RequestParam("send") boolean send) {
        return accountService.sendAuthCodeSMS(request, phone, send);
    }

    @NeedToken
    @GetMapping("/account/refresh")
    public ResponseEntity<ResultBean<LoginUserVM>> refresh(
            @RequestParam("statId") String statId) {
        return accountService.refresh(statId);
    }

    @NoToken
    @PostMapping("/report")
    public ResponseEntity<ResultBean<Void>> report(
            HttpServletRequest request,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "user_id", required = false) String userId,
            @RequestBody String reportContent) {
        String app_channel = request.getHeader("channel");

        return accountService.report(app_channel, phone, userId, reportContent);
    }

    @NeedToken
    @GetMapping("/getReportList")
    public ResponseEntity<ResultBean<List<ReportBean>>> getReportList(
            @RequestParam(value = "user_id") String userId) {
        return accountService.getReportList(userId);
    }

    @NeedToken
    @PostMapping("/user_data")
    public ResponseEntity<ResultBean<Void>> modifyUserData(
            @RequestParam(value = "avatar", required = false) String avatar,
            @RequestParam(value = "nick_name", required = false) String nick_name,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "gender", required = false) int gender) {
        return accountService.modifyUserData(avatar, nick_name, birthday, gender);
    }

    @NeedToken
    @PostMapping("/account/pwd")
    public ResponseEntity<ResultBean<Void>> modifyPwd(
            @RequestParam("pwd_key_md5") String pwd_key_md5) {
        return accountService.modifyPwd(pwd_key_md5);
    }

    @NoToken
    @GetMapping("/signData")
    public ResponseEntity<ResultBean<SignBean>> getSignData() {
        return accountService.getSignData();
    }

    @NeedToken
    @PostMapping("/startSign")
    public ResponseEntity<ResultBean<String>> startSign() {
        return accountService.startSign();
    }


}
